package com.chumi.bootstrap.controller.demo;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Max;

import org.hibernate.validator.constraints.Length;

public class TestValidator {
	@Max(value = 3) // 鏈�ぇ鍊间负3
	private int age;
	@Length(max = 1) // 瀛楃涓查暱搴︽渶澶т负1,hibernate 鎵╁睍鐨�
	private String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void main(String[] args) {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		Validator validator = factory.getValidator();
		TestValidator entity = new TestValidator();
		entity.setAge(12);
		entity.setName("admin");
		Set<ConstraintViolation<TestValidator>> constraintViolations = validator.validate(entity);
		for (ConstraintViolation<TestValidator> constraintViolation : constraintViolations) {
			System.out.println("瀵硅薄灞炴�:" + constraintViolation.getPropertyPath());
			System.out.println("鍥介檯鍖杒ey:" + constraintViolation.getMessageTemplate());
			System.out.println("閿欒淇℃伅:" + constraintViolation.getMessage());
		}
	}
}
